
import 'package:extended_text_library/extended_text_library.dart';
import 'package:flutter/material.dart';
import 'package:osc/common/regex_constants.dart';
import 'package:osc/utils/pd_util.dart';
import 'package:osc/utils/regex_util.dart';

import '../emoji_util.dart';

///emoji/image text
class EmojiText extends SpecialText {
  static const String flagBracketsL = "[";
  static const String flagBracketsR = "]";
  static const String flagColon = ":";
  final int start;

  //fontsize id define image height
  //size = 30.0/26.0 * fontSize
  final double size = 20.0;
  EmojiText({@required TextStyle textStyle, @required String startFlag, @required String endFlag, this.start})
      : super(startFlag, endFlag, textStyle);

  @override
  InlineSpan finishText() {
    String key = toString();
    if (EmojiUitl.instance.emojiMap.containsKey(key)) {
      return createImageSpan(key, EmojiUitl.instance.emojiMap[key]);
    } else if (EmojiUitl.instance.qqEmojiMap.containsKey(key)) {
      return createImageSpan(key, EmojiUitl.instance.qqEmojiMap[key]);
    } else if(RegexUtil.isMatch(RegexConstants.REGEX_EMOJI, key)){
      return createImageSpan(key, "smiley_${key.substring(1, key.length-1)}");
    }

    return TextSpan(text: toString(), style: textStyle);
  }

  ImageSpan createImageSpan(String actualText, String imageName){
    return ImageSpan(AssetImage(PdUtil.getEmojiPath(imageName)),
        actualText: actualText,
        imageWidth: size,
        imageHeight: size,
        start: start,
        fit: BoxFit.fill,
        margin: EdgeInsets.only(left: 2.0, top: 2.0, right: 2.0),
    );
  }
}

